
// 获取token以及header;
$(document).ready(function(){

     tokenLength = location.href.length;
     tokenStart = location.href.indexOf("&");
     nameStart = location.href.lastIndexOf("?");
     name = location.href.slice(nameStart+1,tokenStart);
     token = location.href.slice(tokenStart+1,tokenLength);
     header = "bearer "+token;  //请求header头部



});


//导航栏配置信息，获取登录用户名称
function leftConfig(){
    // var nameStart = location.href.indexOf("?");
    // var nameEnd = location.href.indexOf("&");
    // var leftUserName = decodeURI(location.href.slice(nameStart+1,nameEnd));
    //console.log(leftUserName);
    document.getElementById("leftUserName").innerHTML = "欢迎您:"+decodeURI(name);
}

//跳转搜索页面
function goToSearch(){
    window.location.href="searchBooks/search.html?"+name+"&"+token;
}

//注销用户
function logOut(){

    $.ajax({
        //几个参数需要注意一下
        type: "get",//方法类型
        dataType: "json",//预期服务器返回的数据类型'
        headers:{'authorization':header},
        url: "http://www.inf.ink./api/logout" ,//url
        success: function (result) {
            if (result["code"] == 200) {
                $("#success").css('opacity',100);
                setTimeout(function () {
                    $("#success").css('opacity',0);
                },2500);
                window.location.href = "120.79.183.142/library/login/form-1/login.html";
            }
            else{

            }
        },
        error : function(r) {
            $("#fail").css('opacity',100);
            setTimeout(function () {
                $("#fail").css('opacity',0);
            },2500);
        }
    });
}

//更改密码
function editSecret(){
    if(document.getElementById("editOld").value===document.getElementById("editNew").value){
        document.getElementById("editOld").value="";
        document.getElementById("editOld").setAttribute("placeholder","新密码与旧密码不能一致！");
    }
    else if(document.getElementById("editNew").value!==document.getElementById("editConfirmNew").value){
        document.getElementById("editNew").value="";
        document.getElementById("editConfirmNew").value="";
        document.getElementById("editNew").setAttribute("placeholder","两次填写的密码不一致！");
    }

    else{
        $.ajax({
            //几个参数需要注意一下
            type : "post",//方法类型
            dataType : "json",//预期服务器返回的数据类型'
            headers : {'authorization':header},
            url : "http://www.inf.ink./api/reset" ,//url
            data : $("#formSecret").serialize(),
            success : function (result) {
                if (result["code"] == 200) {
                    $("#success").css('opacity',100);
                    setTimeout(function () {
                        $("#success").css('opacity',0);
                    },2500);
                }
            },
            error : function(r) {
                $("#fail").css('opacity',100);
                setTimeout(function () {
                    $("#fail").css('opacity',0);
                },2500);
            }
        });
    }
}

var getMyInformation = false;
//向后台请求个人信息并显示
function userInformation() {
    if(getMyInformation){
        return void(0);
    }


    $.ajax({
        //几个参数需要注意一下
        type: "get",//方法类型
        dataType: "json",//预期服务器返回的数据类型'
        headers:{'authorization':header},
        url: "http://120.79.183.142/api/user/info" ,//url
        success: function (result) {
            // console.log(result);//打印服务端返回的数据(调试用)
            if (result["code"] == 200) {
                var username = result["data"]["username"];
                document.getElementById("selfUserName").value=username;
                getMyInformation = true;
            }
        },
        error : function(r) {
            $("#fail").css('opacity',100);
            setTimeout(function () {
                $("#fail").css('opacity',0);
            },2500);
            window.location.href = "../../login/form-1/login.html";
        }
    });
}






